﻿#include "configfile.h"
#include <QApplication>
#include <QTextCodec>
#include <QFile>
#include <QDebug>

ConfigFile  * ConfigFile::m_configFile = NULL;
ConfigFile::ConfigFile(QObject *parent) : QObject(parent)
  , m_settings(NULL)
  , m_settingVersions(NULL)
{
    QString docParhFile =  QApplication::applicationDirPath();
    QString docParhFile2 =docParhFile + "/configs/versitons.ini";
    m_docParhFile2 = docParhFile2;
    docParhFile +="/configs/configs.ini";
    m_settings= new QSettings (docParhFile, QSettings ::IniFormat);
    m_settings->setIniCodec(QTextCodec::codecForName("UTF-8"));


}

ConfigFile::~ConfigFile()
{

}

ConfigFile *ConfigFile::gestance()
{
    if(m_configFile == NULL){
        m_configFile = new ConfigFile();
    }

    return  m_configFile;
}

QString ConfigFile::getCom1Name()
{
    QString  coms ="";
    if(m_settings != NULL){
        m_settings->beginGroup("bases");
        coms=  m_settings->value("com1").toString();
        m_settings->endGroup();
    }
    return   coms;
}

QString ConfigFile::getCom2Name()
{
    QString  coms ="";
    if(m_settings != NULL){
        m_settings->beginGroup("bases");
        coms=  m_settings->value("com2").toString();
        m_settings->endGroup();
    }
    return   coms;
}

QString ConfigFile::getCom3Name()
{
    QString  coms ="";
    if(m_settings != NULL){
        m_settings->beginGroup("bases");
        coms=  m_settings->value("com3").toString();
        m_settings->endGroup();
    }
    return   coms;
}

QString ConfigFile::getCom4Name()
{
    QString  coms ="";
    if(m_settings != NULL){
        m_settings->beginGroup("bases");
        coms=  m_settings->value("com4").toString();
        m_settings->endGroup();
    }
    return   coms;
}

QString ConfigFile::getDevices()
{
    QString  devices ="";
    if(m_settings != NULL){
        m_settings->beginGroup("bases");
        devices=  m_settings->value("devices").toString();
        m_settings->endGroup();
    }
    return   devices;
}

QString ConfigFile::getSns()
{
    QString  devices ="";
    if(m_settings != NULL){
        m_settings->beginGroup("bases");
        devices=  m_settings->value("SN").toString();
        m_settings->endGroup();
    }
    return   devices;
}

QString ConfigFile::getVersions()
{
    //    if(m_settingVersions == NULL){
    //        m_settingVersions= new QSettings (m_docParhFile2, QSettings ::IniFormat);
    //        m_settingVersions->setIniCodec(QTextCodec::codecForName("UTF-8"));

    //    }
    //    QString  versions ="";
    //    if(m_settingVersions != NULL){
    //        m_settingVersions->beginGroup("bases");
    //        versions=  m_settingVersions->value("versions").toString();
    //        m_settingVersions->endGroup();
    //    }
    QString  versions ="";
    QFile files(m_docParhFile2);
    if(files.open(QIODevice::ReadOnly)){
        QString cnts = files.readAll();
        QStringList litsa=  cnts.split("\n");
        for(int i =0 ;i < litsa.size();i++ ){
            QString itemsa= litsa.at(i);
            if(itemsa.contains("versions")){
              itemsa =  itemsa.replace("versions","");
              itemsa =  itemsa.replace("\n","");
              itemsa =  itemsa.replace("\r","");
              itemsa =  itemsa.replace("=","");
              itemsa =  itemsa.replace(" ","");
              versions =itemsa;
            }
        }
    }
   // qDebug()<<__func__<<__LINE__<<versions;
    return   versions;
}
